﻿#include "PlayerOnQMediaBL.h"
#include "PlayerOnFFMpeg.h"
#include "PlayerOnQMedia.h"
#include "Utils/QPTypes.h"

#include <QMessageBox>
///
/// \brief PlayerOnQMediaBL::PlayerOnQMediaBL
/// \param type 由调用者自行判断是否传入参数，但是在此处依然默认使用qt自带的
///
PlayerOnQMediaBL::PlayerOnQMediaBL(QObject *parent)
    :QObject{parent}
{
    abs_Player = new PlayerOnQMedia;//默认引擎
}


void PlayerOnQMediaBL::setEngine(QP::PlayEngine type)
{
    EngineType = type;
    switch (type) {
    case QP::QTMEDIA:
        abs_Player = new PlayerOnQMedia;
        break;
    case QP::FFMPEG:
        abs_Player = new PlayerOnFFMpeg;
        break;
    default:
        abs_Player = new PlayerOnQMedia;
        break;
    }
}

QP::PlayEngine PlayerOnQMediaBL::Engine()
{
    return EngineType;
}

PlayerOnQMediaBL::~PlayerOnQMediaBL()
{
    delete abs_Player;
}

bool PlayerOnQMediaBL::open(const QString &media)
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return false;
    }
    return abs_Player->open(media);
}

bool PlayerOnQMediaBL::play()
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return false;
    }
    bool isPlaying = abs_Player->play();
    if(isPlaying)
    {
        setState(QP::PlayingState);
        return true;
    }
    setState(QP::StoppedState);
    return false;
}

bool PlayerOnQMediaBL::pause()
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return false;
    }
    bool isPlaying = abs_Player->pause();
    if(isPlaying)
    {
        setState(QP::PausedState);
        return true;
    }
    setState(QP::StoppedState);
    return false;
}

bool PlayerOnQMediaBL::stop()
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return false;
    }
    bool isPlaying = abs_Player->stop();
    if(isPlaying)
    {
        setState(QP::StoppedState);
        return true;
    }
    return false;
}

QP::PlaybackState PlayerOnQMediaBL::state() const
{
    return PlayState;
}

void PlayerOnQMediaBL::setState(QP::PlaybackState state)
{
    PlayState = state;
}

float PlayerOnQMediaBL::volume() const
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return false;
    }
    return abs_Player->volume();
}

void PlayerOnQMediaBL::setVolume(float volume)
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return;
    }
    abs_Player->setVolume(volume);
}

bool PlayerOnQMediaBL::isMuted() const
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return false;
    }
    return abs_Player->isMuted();
}

void PlayerOnQMediaBL::setMuted(bool muted)
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return;
    }
    abs_Player->setMuted(muted);
}

void PlayerOnQMediaBL::setPlaybackRate(float rate)
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return;
    }
    abs_Player->setPlaybackRate(rate);
}

void PlayerOnQMediaBL::setVideoWidget(QObject *w)
{
    if(!abs_Player)
    {
        QMessageBox::warning(nullptr,"错误","请选择播放引擎");
        return;
    }
    abs_Player->setVideoWidget(w);
}


